package com.cn.dim.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cn.dim.pojo.Pages;
import com.cn.dim.pojo.Position;
import com.cn.dim.service.PositionService;
import com.github.pagehelper.Page;


@Controller
@RequestMapping("/position")
public class PositionController {

	@Resource
	private PositionService positionService;

	@RequestMapping(value ="/queryPostion",method = RequestMethod.GET)
	@ResponseBody
	public Map<String,Object> queryPostion(HttpServletRequest request,Position p,Pages pages){
		Map<String,Object> param=new HashMap<>();
		Map<String,Object> reMap=new HashMap<>();
		param.put("posnum", p.getPosnum());//这个就是xml文件中引用的变量，如果保存的是实体，则在xml文件中需要使用“实体.字段”的方式
		param.put("posname", p.getPosname());
		param.put("pageIndex", pages.getPage());
		param.put("pageSize", pages.getRows());

		try {
			Page<Map<String,Object>> rList = (Page)positionService.queryPostion(param);
			reMap.put("rows", rList);
			reMap.put("total", rList.getTotal());
			reMap.put("status", "success");
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			reMap.put("status", "error");
		}
		return reMap;
	}
	@RequestMapping(value ="/insertPostion",method = RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> insertPostion(Position p){
		Map<String,Object> reMap=new HashMap<>();
		p.setId(com.cn.dim.utils.StringUtil.makeUUID());
		p.setIsdel("0");
		
		//这里补充创建人的id的代码
		
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date cdate = sdf.parse(sdf.format(new Date()));//创建日期
			p.setCreatetime(cdate);
			int flag = positionService.insertPosition(p);
			if(flag>0){
				reMap.put("message", "插入成功！");
				reMap.put("status", "success");
			}else{
				reMap.put("status", "error");
			}
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			reMap.put("status", "error");
		}
		return reMap;
	}
	
	@RequestMapping(value ="/delPostion",method = RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> delPostion(Position p){
		Map<String,Object> reMap=new HashMap<>();
		Map<String,Object> params=new HashMap<>();
		params.put("id", p.getId());
		
		//这里补充修改（删除）人的id的代码
		
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date cdate = sdf.parse(sdf.format(new Date()));//修改（删除）日期
			params.put("updatetime", cdate);
			int flag = positionService.delPosition(params);
			if(flag>0){
				reMap.put("message", "删除成功！");
				reMap.put("status", "success");
			}else{
				reMap.put("status", "error");
			}
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			reMap.put("status", "error");
		}
		return reMap;
	}
	
	@RequestMapping(value ="/getPositionById",method = RequestMethod.GET)
	@ResponseBody
	public Map<String,Object> getPositionById(HttpServletRequest request,Position p){
		Map<String,Object> param=new HashMap<>();
		Map<String,Object> reMap=new HashMap<>();
		param.put("id", p.getId());

		try {
			Position rList = positionService.getPositionById(param);
			reMap.put("data", rList);
			reMap.put("status", "success");
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			reMap.put("status", "error");
		}
		return reMap;
	}
	
	@RequestMapping(value ="/updatePostion",method = RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> updatePostion(Position p){
		Map<String,Object> reMap=new HashMap<>();
		Map<String,Object> params=new HashMap<>();
		params.put("id", p.getId());
		params.put("posname", p.getPosname());
		params.put("posnum", p.getPosnum());
		
		//这里补充修改人的id的代码
		
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date cdate = sdf.parse(sdf.format(new Date()));//修改日期
			params.put("updatetime", cdate);
			int flag = positionService.updatePosition(params);
			if(flag>0){
				reMap.put("message", "修改成功！");
				reMap.put("status", "success");
			}else{
				reMap.put("status", "error");
			}
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			reMap.put("status", "error");
		}
		return reMap;
	}
	@RequestMapping(value ="/queryAllPostion",method = RequestMethod.GET)
	@ResponseBody
	public List<Map<String,Object>> queryAllPostion(HttpServletRequest request){
		Map<String,Object> param=new HashMap<>();
		List<Map<String, Object>> rList = new ArrayList<>();
		try {
			rList = positionService.queryAllPostion(param);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return rList;
	}
}
